You're reading an old version of this documentation.
For the latest stable release version, please have a look at master.

NullPointerException

Introduction

NullPointerException is a Scala runtime reported error which can happen when a variable is accessed before it has been initialized.

Example

The following code:

class TopLevel extends Component {
  a := 42
  val a = UInt(8 bits)
}

will throw:

Exception in thread "main" java.lang.NullPointerException
  ***
  Source file location of the a := 42 assignment via the stack trace
  ***

A fix could be:

class TopLevel extends Component {
  val a = UInt(8 bits)
  a := 42
}

Issue explanation

SpinalHDL is not a language, it is a Scala library, which means that it obeys the same rules as the Scala general purpose programming language.

When running the above SpinalHDL hardware description to generate the corresponding VHDL/Verilog RTL, the SpinalHDL hardware description will be executed as a Scala program, and a will be a null reference until the program executes val a = UInt(8 bits), so trying to assign to it before then will result in a NullPointerException.